package Controller.ArticleController;

import Model.Article;
import Service.ArticleService;
import Utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Article/delete.do")
public class DelArticleController extends HttpServlet {

    ArticleService articleService= ServiceFactory.getArticleService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        Article article = articleService.findArticleById(id);
        System.out.println(article);
        boolean a=articleService.delArticle(article,id);
        if(a){
            resp.getWriter().println("<script> alert('删除成功!');window.location.href='/back-page/article.html' </script>");
        }else{
            resp.getWriter().println("<script> alert('不可能，居然会删除失败？？？？？');window.location.href='/back-page/article.html' </script>");

        }
    }
}
